{% extends "base_generic.html" %}

{% block content %}
  <div class="detail-header">
    <h1><i class="fas fa-user me-2"></i>个人资料</h1>
    <p class="lead">管理您的个人信息和阅读偏好</p>
  </div>
  
  <div class="row">
    <div class="col-md-4">
      <div class="detail-section">
        <div class="text-center mb-4">
          {% if user.profile.avatar %}
            <img src="{{ user.profile.avatar.url }}" alt="{{ user.username }}" class="img-fluid rounded-circle" style="width: 150px; height: 150px; object-fit: cover;">
          {% else %}
            <div class="rounded-circle bg-secondary d-flex align-items-center justify-content-center mx-auto" style="width: 150px; height: 150px;">
              <i class="fas fa-user fa-5x text-white"></i>
            </div>
          {% endif %}
          <h3 class="mt-3">{{ user.get_full_name|default:user.username }}</h3>
          <p class="text-muted">{{ user.email }}</p>
        </div>
        
        <h4>阅读偏好</h4>
        {% if user.profile.reading_preferences.exists %}
          <div class="mb-3">
            {% for genre in user.profile.reading_preferences.all %}
              <span class="badge bg-primary mb-1">{{ genre.name }}</span>
            {% endfor %}
          </div>
        {% else %}
          <p class="text-muted">您尚未设置阅读偏好</p>
        {% endif %}
        
        <h4>个人简介</h4>
        <p>{{ user.profile.bio|default:"暂无简介" }}</p>
      </div>
    </div>
    
    <div class="col-md-8">
      <ul class="nav nav-tabs mb-4" id="profileTabs" role="tablist">
        <li class="nav-item" role="presentation">
          <button class="nav-link active" id="edit-tab" data-bs-toggle="tab" data-bs-target="#edit" type="button" role="tab" aria-controls="edit" aria-selected="true">编辑资料</button>
        </li>
        <li class="nav-item" role="presentation">
          <button class="nav-link" id="favorites-tab" data-bs-toggle="tab" data-bs-target="#favorites" type="button" role="tab" aria-controls="favorites" aria-selected="false">收藏的书籍</button>
        </li>
        <li class="nav-item" role="presentation">
          <button class="nav-link" id="history-tab" data-bs-toggle="tab" data-bs-target="#history" type="button" role="tab" aria-controls="history" aria-selected="false">借阅历史</button>
        </li>
      </ul>
      
      <div class="tab-content" id="profileTabsContent">
        <div class="tab-pane fade show active" id="edit" role="tabpanel" aria-labelledby="edit-tab">
          <div class="detail-section">
            <h4>编辑个人资料</h4>
            <form method="post" enctype="multipart/form-data">
              {% csrf_token %}
              
              <div class="row mb-3">
                <div class="col-md-6">
                  <label for="{{ user_form.first_name.id_for_label }}" class="form-label">名</label>
                  {{ user_form.first_name }}
                </div>
                <div class="col-md-6">
                  <label for="{{ user_form.last_name.id_for_label }}" class="form-label">姓</label>
                  {{ user_form.last_name }}
                </div>
              </div>
              
              <div class="mb-3">
                <label for="{{ user_form.email.id_for_label }}" class="form-label">电子邮箱</label>
                {{ user_form.email }}
              </div>
              
              <div class="mb-3">
                <label for="{{ profile_form.avatar.id_for_label }}" class="form-label">头像</label>
                {{ profile_form.avatar }}
              </div>
              
              <div class="mb-3">
                <label for="{{ profile_form.bio.id_for_label }}" class="form-label">个人简介</label>
                {{ profile_form.bio }}
              </div>
              
              <div class="mb-3">
                <label for="{{ profile_form.reading_preferences.id_for_label }}" class="form-label">阅读偏好</label>
                <div class="border rounded p-3">
                  {{ profile_form.reading_preferences }}
                </div>
              </div>
              
              <button type="submit" class="btn btn-primary">保存更改</button>
            </form>
          </div>
        </div>
        
        <div class="tab-pane fade" id="favorites" role="tabpanel" aria-labelledby="favorites-tab">
          <div class="detail-section">
            <h4>收藏的书籍</h4>
            {% if favorite_books %}
              <div class="row">
                {% for book in favorite_books %}
                  <div class="col-md-6 mb-3">
                    <div class="card">
                      <div class="row g-0">
                        <div class="col-4">
                          <div class="book-card-img h-100">
                            {% if book.cover %}
                              <img src="{{ book.cover.url }}" alt="{{ book.title }}" class="img-fluid h-100 w-100" style="object-fit: cover;">
                            {% else %}
                              <div class="d-flex align-items-center justify-content-center h-100">
                                <i class="fas fa-book fa-3x"></i>
                              </div>
                            {% endif %}
                          </div>
                        </div>
                        <div class="col-8">
                          <div class="card-body">
                            <h5 class="card-title">{{ book.title }}</h5>
                            <p class="card-text">{{ book.author }}</p>
                            <a href="{{ book.get_absolute_url }}" class="btn btn-primary btn-sm">查看详情</a>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                {% endfor %}
              </div>
            {% else %}
              <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>您还没有收藏任何书籍
              </div>
            {% endif %}
          </div>
        </div>
        
        <div class="tab-pane fade" id="history" role="tabpanel" aria-labelledby="history-tab">
          <div class="detail-section">
            <h4>借阅历史</h4>
            {% if borrow_history %}
              <div class="table-responsive">
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th>书名</th>
                      <th>借阅日期</th>
                      <th>归还日期</th>
                      <th>状态</th>
                    </tr>
                  </thead>
                  <tbody>
                    {% for history in borrow_history %}
                      <tr>
                        <td>
                          <a href="{{ history.book_instance.book.get_absolute_url }}">
                            {{ history.book_instance.book.title }}
                          </a>
                        </td>
                        <td>{{ history.borrow_date }}</td>
                        <td>{{ history.return_date|default:"尚未归还" }}</td>
                        <td>
                          {% if history.return_date %}
                            <span class="badge bg-success">已归还</span>
                          {% else %}
                            <span class="badge bg-warning">借阅中</span>
                          {% endif %}
                        </td>
                      </tr>
                    {% endfor %}
                  </tbody>
                </table>
              </div>
            {% else %}
              <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>您还没有借阅记录
              </div>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
  </div>
{% endblock %} 